﻿using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.ProcessRoute;
using PowerHub.MES.IRepository.IProcessRoute;
using PowerHub.MES.DTO.ProcessRoute.QueryDto;
using Microsoft.EntityFrameworkCore;

namespace PowerHub.MES.Repository.ProcessRoute
{
    /// <summary>
    /// 工序数据实现层
    /// </summary>
    public class ProcessRoute_ProcessRepository : BaseRepository<ProcessRoute_Process>, IProcessRoute_ProcessRepository
    {
        private PowerHubMesContext _repositorySys;
        public ProcessRoute_ProcessRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加工序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProcessRoute_ProcessAsync(ProcessRoute_Process query)
        {
            var result = new OperationResult<bool>();
            if (AddEntity(query) > 0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProcessRoute_ProcessAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }

        /// <summary>
        /// 获取工序列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProcessRoute_Process>>> GetProcessRoute_ProcessPageListAsync(QueryProcessRoute_ProcessDTO query)
        {
            var result = new OperationPageResult<List<ProcessRoute_Process>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                            .Where(p => p.IsDel == 0).Where(p => p.ProcessName
                                            .Contains(query.ProcessName ?? string.Empty));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderByDescending(g => g.Createtime).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 更新工序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProcessRoute_ProcessAsync(ProcessRoute_Process query)
        {
            var result = new OperationResult<bool>();
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return result;
        }
    }
}
